<?php
class SF_VendorAnnouncement_IndexController extends Mage_Adminhtml_Controller_Action
{
	protected function _isAllowed()
	{
		return true;
	}
	
	public function indexAction()
	{
		$this->loadLayout();
		$this->renderLayout();
	}
	
	/**
	 * 修改公告主题
	 */
	public function editTypeAction()
	{
		$typeId = (int)$this->getRequest()->getParam('id');
		$typeName = $this->getRequest()->getParam('name');
		$writer = Mage::getSingleton('core/resource')->getConnection('core_write');
		$table = Mage::getSingleton('core/resource')->getTableName('rumall_vendor_announcement_type');
		$affectedCount = $writer->update($table, array('name' => $typeName), array('type_id=?' => $typeId)); //返回受影响行数
		echo $affectedCount;
	}
	
	/**
	 * 创建公告主题
	 */
	public function createTypeAction()
	{
		$typeName = $this->getRequest()->getParam('name');
		$writer = Mage::getSingleton('core/resource')->getConnection('core_write');
		$table = Mage::getSingleton('core/resource')->getTableName('rumall_vendor_announcement_type');
		$affectedCount = $writer->insert($table, array('name' => $typeName)); //返回受影响行数
		if ($affectedCount == 1)
			echo $writer->lastInsertId();
		else
			echo 0;
	}
	
	/**
	 * 查询所有公告主题
	 */
	public function getTypesAction()
	{
		$result = array('status' => 0, 'data' => array());
		$reader = Mage::getSingleton('core/resource')->getConnection('core_read');
		$sql = 'SELECT type_id id,name FROM rumall_vendor_announcement_type WHERE del_flag=0';
		$result['data'] = $reader->fetchAll($sql);
		if (!empty($result['data'])) {
			$result['status'] = 1;
		}
		echo json_encode($result);
	}
	
	/**
	 * 创建公告
	 */
	public function createAnnouncementAction()
	{
		$title = htmlspecialchars($this->getRequest()->getParam('title'));
		$type = (int)$this->getRequest()->getParam('type');
		$status = (int)$this->getRequest()->getParam('status', 0);
		$isTop = (int)$this->getRequest()->getParam('isTop', 0);
		$content = $this->getRequest()->getParam('content');
		
		$writer = Mage::getSingleton('core/resource')->getConnection('core_write');
		$table = Mage::getSingleton('core/resource')->getTableName('rumall_vendor_announcement');
		$time = time();
		$admin = Mage::getSingleton('admin/session')->getUser()->getUsername();
		$data = array();
		$data['create_by'] = $admin;
		$data['edit_by'] = $admin;
		$data['title'] = $title;
		$data['type_id'] = $type;
		$data['status'] = $status; //0-草稿 1-发布中 2-已失效
		$data['is_top'] = $isTop;
		$data['content'] = $content;
		$data['create_time'] = $time;
		$data['edit_time'] = $time;
		$data['for_all'] = 1;
		$data['for_vendors'] = '';
		$affectedCount = $writer->insert($table, $data); //返回受影响行数
		echo $affectedCount;
	}
	
	/**
	 * 获取公告列表
	 */
	public function getAnnouncementsBySortAction()
	{
		$type = $this->getRequest()->getParam('type');
		$status = $this->getRequest()->getParam('status');
		$isTop = $this->getRequest()->getParam('isTop');
		$creator = $this->getRequest()->getParam('creator');
		$beginDate = $this->getRequest()->getParam('beginDate');
		$endDate = $this->getRequest()->getParam('endDate');
		$page = (int)$this->getRequest()->getParam('page', 1);
		$pageSize = (int)$this->getRequest()->getParam('pageSize', 10);
	
		$result = array('total' => 0, 'data' => array());
		$reader = Mage::getSingleton('core/resource')->getConnection('core_read');
	
		$condition1 = array();
		$condition2 = array();
		if (!empty($type))
		{
			$type = (int)$type;
			$condition1[] = "type_id={$type}";
			$condition2[] = "a.type_id={$type}";
		}
		if (!empty($status))
		{
			$status = (int)$status;
			--$status;
			$condition1[] = "status={$status}";
			$condition2[] = "a.status={$status}";
		}
		if (!empty($isTop))
		{
			$isTop = (int)$isTop;
			--$isTop;
			$condition1[] = "is_top={$isTop}";
			$condition2[] = "a.is_top={$isTop}";
		}
		if (!empty($creator))
		{
			$creator = addslashes($creator);
			$condition1[] = "create_by='{$creator}'";
			$condition2[] = "a.create_by='{$creator}'";
		}
		if (!empty($beginDate) && !empty($endDate) && preg_match('/^\d{4}-\d{2}-\d{2}$/', $beginDate) == 1 && preg_match('/^\d{4}-\d{2}-\d{2}$/', $endDate) == 1)
		{
			$beginDate = strtotime($beginDate);
			$endDate = strtotime($endDate) + 86399;
			$condition1[] = "edit_time BETWEEN {$beginDate} AND {$endDate}";
			$condition2[] = "a.edit_time BETWEEN {$beginDate} AND {$endDate}";
		}
		$condition1 = count($condition1) > 0 ? (' WHERE ' . implode(' AND ', $condition1)) : '';
		$condition2 = count($condition2) > 0 ? (' AND ' . implode(' AND ', $condition2)) : '';
		
		$sql = "SELECT count(id) total FROM rumall_vendor_announcement {$condition1}";
		$row = $reader->fetchRow($sql);
		$result['total'] = is_array($row) ? $row['total'] : 0;
		
		$sql = "SELECT a.*,b.name
				FROM rumall_vendor_announcement a
				INNER JOIN rumall_vendor_announcement_type b
				ON a.type_id=b.type_id AND b.del_flag=0
				{$condition2}
				ORDER by a.is_top DESC,a.id DESC
				LIMIT " . (($page - 1) * $pageSize) . ',' . $pageSize;
		$data = $reader->fetchAll($sql);
		
		if (!empty($data))
		{
			for ($i = 0, $len = count($data); $i < $len; ++$i)
			{
				$data[$i]['create_time'] = date('Y-m-d H:i:s', $data[$i]['create_time']);
				$data[$i]['edit_time'] = date('Y-m-d H:i:s', $data[$i]['edit_time']);
				$data[$i]['content'] = preg_replace('/<.*?>/', '', $data[$i]['content']);
				$data[$i]['content'] = mb_strlen($data[$i]['content'], 'utf-8') > 100 ? (mb_substr($data[$i]['content'], 0, 100, 'utf-8') . '...') : $data[$i]['content'];
			}
			$result['data'] = $data;
		}
		
		echo json_encode($result);
	}
	
	/**
	 * 获取公告列表
	 */
	public function getAnnouncementsBySearchAction()
	{
		$keyword = addslashes($this->getRequest()->getParam('keyword'));
		$page = (int)$this->getRequest()->getParam('page', 1);
		$pageSize = (int)$this->getRequest()->getParam('pageSize', 10);
		
		$result = array('total' => 0, 'data' => array());
		$reader = Mage::getSingleton('core/resource')->getConnection('core_read');
	
		if ($keyword === '')
		{
			$condition1 = '';
			$condition2 = '';
		}
		else
		{
			$condition1 = " WHERE title LIKE '%{$keyword}%' OR content LIKE '%{$keyword}%' ";
			$condition2 = " AND a.title LIKE '%{$keyword}%' OR a.content LIKE '%{$keyword}%' ";
		}
		$sql = "SELECT count(id) total FROM rumall_vendor_announcement {$condition1}";
		$row = $reader->fetchRow($sql);
		$result['total'] = is_array($row) ? $row['total'] : 0;
	
		$sql = "SELECT a.*,b.name 
				FROM rumall_vendor_announcement a 
				INNER JOIN rumall_vendor_announcement_type b 
				ON a.type_id=b.type_id AND b.del_flag=0 
				{$condition2} 
				ORDER by a.is_top DESC,a.id DESC 
				LIMIT " . (($page - 1) * $pageSize) . ',' . $pageSize;
		$data = $reader->fetchAll($sql);
		
		if (!empty($data))
		{
			for ($i = 0, $len = count($data); $i < $len; ++$i)
			{
				$data[$i]['create_time'] = date('Y-m-d H:i:s', $data[$i]['create_time']);
				$data[$i]['edit_time'] = date('Y-m-d H:i:s', $data[$i]['edit_time']);
				$data[$i]['content'] = preg_replace('/<.*?>/', '', $data[$i]['content']);
				$data[$i]['content'] = mb_strlen($data[$i]['content'], 'utf-8') > 100 ? (mb_substr($data[$i]['content'], 0, 100, 'utf-8') . '...') : $data[$i]['content'];
			}
			$result['data'] = $data;
		}
		
		echo json_encode($result);
	}
	
	/**
	 * 查询公告详细
	 */
	public function getAnnouncementDetailAction()
	{
		$id = (int)$this->getRequest()->getParam('id');
		$result = array('status' => 0, 'data' => array());
		$reader = Mage::getSingleton('core/resource')->getConnection('core_read');
		$sql = 'SELECT a.*,b.name
				FROM rumall_vendor_announcement a
				INNER JOIN rumall_vendor_announcement_type b
				ON a.type_id=b.type_id AND a.id=' . $id;
		$result['data'] = $reader->fetchRow($sql);
		if (!empty($result['data'])) {
			$result['status'] = 1;
		}
		echo json_encode($result);
	}
	
	/**
	 * 编辑公告
	 */
	public function editAnnouncementAction()
	{
		$id = (int)$this->getRequest()->getParam('id');
		$type = (int)$this->getRequest()->getParam('type');
		$title = htmlspecialchars($this->getRequest()->getParam('title'));
		$status = (int)$this->getRequest()->getParam('status', 0);
		$isTop = (int)$this->getRequest()->getParam('isTop', 0);
		$content = $this->getRequest()->getParam('content');
		
		$writer = Mage::getSingleton('core/resource')->getConnection('core_write');
		$table = Mage::getSingleton('core/resource')->getTableName('rumall_vendor_announcement');
		$data = array();
		$data['title'] = $title;
		$data['type_id'] = $type;
		$data['status'] = $status;
		$data['is_top'] = $isTop;
		$data['content'] = $content;
		$data['edit_by'] = Mage::getSingleton('admin/session')->getUser()->getUsername();
		$data['edit_time'] = time();
		$affectedCount = $writer->update($table, $data, array('id=?' => $id)); //返回受影响行数
		echo $affectedCount;
	}
	
	/**
	 * 获取所有公告创建人
	 */
	public function getCreatorsAction()
	{
		$result = array('error' => 1, 'data' => array());
		$reader = Mage::getSingleton('core/resource')->getConnection('core_read');
		$sql = 'SELECT DISTINCT create_by FROM rumall_vendor_announcement';
		$rows = $reader->fetchAll($sql);
		if (is_array($rows))
		{
			foreach ($rows as $row) {
				$result['data'][] = $row['create_by'];
			}
			$result['error'] = 0;
		}
		echo json_encode($result);
	}
}